package com.yksj.healthtalk.adapter;
import java.util.ArrayList;
import java.util.List;

import org.universalimageloader.core.ImageLoader;

import android.app.Service;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.ForbidWordEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.chatting.ForbiddenWordsListActivity;

public class ForbiddenWordsAdapter extends BaseAdapter {
	final List<ForbidWordEntity> listEntities = new ArrayList<ForbidWordEntity>();
	LayoutInflater mLayoutInflater;
	ImageLoader mImageLoader;
	String overTime;//
	Context context;
	
	public ForbiddenWordsAdapter(Context context){
		this.context = context;
		mLayoutInflater = (LayoutInflater)context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
		mImageLoader = ImageLoader.getInstance();
		overTime = "剩余时间%d分钟";
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listEntities.size();
	}

	@Override
	public ForbidWordEntity getItem(int position) {
		// TODO Auto-generated method stub
		return listEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	public void addData(List<ForbidWordEntity> list){
		listEntities.clear();
		listEntities.addAll(list);
		notifyDataSetChanged();
	}
	
	public List<ForbidWordEntity> getDataList(){
		return listEntities;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.forbidden_words_list_item,null);
			viewHolder.headImagV = (ImageView)convertView.findViewById(R.id.user_head);
			viewHolder.sexImgV = (ImageView)convertView.findViewById(R.id.head_image_sex);
			viewHolder.nameTxtV = (TextView)convertView.findViewById(R.id.duomeihao_tv);
			viewHolder.timeTxtV = (TextView)convertView.findViewById(R.id.time_tv);
			viewHolder.switchCheckBox = (CheckBox)convertView.findViewById(R.id.forbidenword_switch);
			convertView.findViewById(R.id.head_image_sex);
			
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		bindData(getItem(position),viewHolder);
		
		return convertView;
	}
	
	private void bindData(ForbidWordEntity forbidWordEntity,ViewHolder viewHolder){
		String time = forbidWordEntity.getTime();
		if(!"".equals(forbidWordEntity.getTime())&& !"0".equals(forbidWordEntity.getTime())){
			int times = 0;
			try{
				times = Integer.valueOf(time);
			}catch(NumberFormatException e){
				e.printStackTrace();
			}
			time = overTime.format(overTime,times);
			viewHolder.timeTxtV.setText(time);
			viewHolder.switchCheckBox.setChecked(true);
		}else{
			viewHolder.timeTxtV.setText(null);
			viewHolder.switchCheckBox.setChecked(false);
		}
		mImageLoader.displayImage(forbidWordEntity.getSex(), forbidWordEntity.getHeaderPath(), viewHolder.headImagV);
		String sex = forbidWordEntity.getSex();
		
		if("M".equals(sex)){
			viewHolder.sexImgV.setImageResource(R.drawable.man);
		}else if("W".equals(sex)){
			viewHolder.sexImgV.setImageResource(R.drawable.women);
		}else{
			viewHolder.sexImgV.setImageResource(0);
		}
		
		String name = forbidWordEntity.getName();
		String duomeiNumber = forbidWordEntity.getDuomeiNumber();
		viewHolder.nameTxtV.setText(name+"("+duomeiNumber+")");
		viewHolder.switchCheckBox.setOnClickListener(new SwitchOnClickListener(forbidWordEntity));
	}
	
	class SwitchOnClickListener implements View.OnClickListener{
		ForbidWordEntity forbidWordEntity;
		SwitchOnClickListener(ForbidWordEntity forbidWordEntity){
			this.forbidWordEntity = forbidWordEntity;
		}
		@Override
		public void onClick(View v) {
			CheckBox checkBox = (CheckBox)v;
			ForbiddenWordsListActivity forbiddenWordsListActivity = (ForbiddenWordsListActivity)context;
			forbiddenWordsListActivity.onSwitchClick(forbidWordEntity,checkBox);
		}
	}
	
	static class ViewHolder{
		ImageView headImagV;
		ImageView sexImgV;
		TextView nameTxtV;
		TextView timeTxtV;
		CheckBox switchCheckBox;
	}

}
